package com.wl.jetpackdemo.chat

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.wl.jetpackdemo.R
import com.wl.jetpackdemo.databinding.ActivityMessageBinding
import com.wl.jetpackdemo.chat.emoji.EmojiAdapter
import com.wl.jetpackdemo.chat.emoji.EmojiUtils
import com.wl.jetpackdemo.chat.emoji.Emoticon
import com.wl.jetpackdemo.base.utils.KeyboardUtils

class MessageActivity : AppCompatActivity() {

    companion object {
        const val INPUT_MODE_NORMAL = 0 //正常文本输入模式
        const val INPUT_MODE_AUDIO = 1 // 语音录制模式

        const val INPUT_EMOJI_HIDE = 0 // 隐藏表情布局
        const val INPUT_EMOJI_SHOW = 1 // 显示表情布局
    }
    private lateinit var binding: ActivityMessageBinding
    private lateinit var adapter: MessageAdapter
    private var inputMode: Int = INPUT_MODE_NORMAL
    private var emojiMode: Int = INPUT_EMOJI_HIDE

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMessageBinding.inflate(layoutInflater)
        setContentView(binding.root)

        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        title = "张三"

        // 初始化底部输入布局
        binding.chatInputTextField.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val text = binding.chatInputTextField.text.toString()
                if(TextUtils.isEmpty(text)) {
                    binding.chatSendEmojiButton.visibility = View.GONE
                } else {
                    binding.chatSendEmojiButton.visibility = View.GONE
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })

        binding.rvList.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
                if (event?.action == MotionEvent.ACTION_UP) {
                    KeyboardUtils.hideInputKeyboard(this@MessageActivity, binding.chatInputTextField)
                    binding.chatInputTextField.clearFocus()
                    return false
                }
                return false
            }

        })

        initBottomAction()

        binding.rvList.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, true)
        adapter = MessageAdapter()
        binding.rvList.adapter = adapter

        val list = mockMsgList()

        adapter.setNewInstance(list)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId) {
            android.R.id.home -> finish()
        }

        return super.onOptionsItemSelected(item)
    }

    fun initBottomAction() {
        binding.chatStreamAudioButton.setOnClickListener {
            if(inputMode == INPUT_MODE_NORMAL) {
                // 隐藏键盘
                KeyboardUtils.hideInputKeyboard(this@MessageActivity, binding.chatInputTextField)
                //切换到语音模式
                changeAudioMode(INPUT_MODE_AUDIO)
                //语音模式 emoji重置回键盘输入模式
                changeEmojiMode(INPUT_EMOJI_HIDE)
            } else {
                changeAudioMode(INPUT_MODE_NORMAL)
            }

            displayExtandLayout(false)

            rvScrollToBottom()
        }

        // emoji layout
        EmojiUtils.parseConfigFile(this.applicationContext)
        binding.rvListEmoji.layoutManager = GridLayoutManager(this, 5)
        var emojiAdapter = EmojiAdapter()
        binding.rvListEmoji.adapter = emojiAdapter
        emojiAdapter.setNewInstance(EmojiUtils.getAllEmojiData() as MutableList<Emoticon>?)

        binding.chatEmojiButton.setOnClickListener {
            if(emojiMode == INPUT_EMOJI_HIDE) {
                // 隐藏键盘
                binding.chatInputTextField.clearFocus()
                KeyboardUtils.hideInputKeyboard(this@MessageActivity, binding.chatInputTextField)

                // 显示emoji
                changeEmojiMode(INPUT_EMOJI_SHOW)

                changeAudioMode(INPUT_MODE_NORMAL)
            } else {

                // 显示键盘
                binding.chatInputTextField.requestFocus()
                KeyboardUtils.showInputKeyboard(binding.chatInputTextField)
                // 隐藏emoji
                changeEmojiMode(INPUT_EMOJI_HIDE)
            }
            displayExtandLayout(false)
        }

        binding.chatInputTextField.setOnTouchListener((View.OnTouchListener { v, event -> //显示键盘
            binding.chatInputTextField.requestFocus()
            KeyboardUtils.showInputKeyboard(binding.chatInputTextField)

            changeEmojiMode(INPUT_EMOJI_HIDE)

            displayExtandLayout(false)

            rvScrollToBottom()
            false
        }))

        //拓展消息
        binding.chatAddButton.setOnClickListener {
            if(binding.chatButtonsLayout.visibility == View.GONE) {
                binding.chatInputTextField.clearFocus()
                KeyboardUtils.hideInputKeyboard(this@MessageActivity, binding.chatInputTextField)
                displayExtandLayout(true)
                if(emojiMode == INPUT_EMOJI_SHOW) {
                    changeEmojiMode(INPUT_EMOJI_HIDE)
                }
                if(inputMode == INPUT_MODE_AUDIO) {
                    changeAudioMode(INPUT_MODE_NORMAL)
                }
            } else {
                displayExtandLayout(false)
            }
        }
    }

    fun rvScrollToBottom() {
        binding.rvList.scrollToPosition(0)
    }

    fun displayExtandLayout(show: Boolean) {
        if(show) {
            binding.chatButtonsLayout.postDelayed({binding.chatButtonsLayout.visibility = View.VISIBLE}, 100)
        } else {
            binding.chatButtonsLayout.visibility = View.GONE
        }
    }

    fun changeAudioMode(mode: Int) {
        inputMode = mode
        if(mode == INPUT_MODE_AUDIO) {
            binding.chatStreamAudioButton.setImageResource(R.drawable.chatting_keyboard_btn_normal)
            binding.chatInputAudioField.visibility = View.VISIBLE
            binding.chatInputTextField.visibility = View.GONE
        } else {
            binding.chatStreamAudioButton.setImageResource(R.drawable.chatting_voice_btn_normal)
            binding.chatInputAudioField.visibility = View.GONE
            binding.chatInputTextField.visibility = View.VISIBLE
        }
    }

    fun changeEmojiMode(mode: Int) {
        emojiMode = mode

        if(emojiMode == INPUT_EMOJI_SHOW) {
            binding.chatEmojiLayout.postDelayed({ binding.chatEmojiLayout.visibility = View.VISIBLE }, 200)
            binding.chatEmojiButton.setImageResource(R.drawable.chatting_keyboard_btn_normal)
        } else {
            binding.chatEmojiLayout.visibility = View.GONE
            binding.chatEmojiButton.setImageResource(R.drawable.chatting_emoji_btn_normal)
        }
    }

    fun mockMsgList(): ArrayList<MultiItemEntity>{
        val list = ArrayList<MultiItemEntity>()
        var msg = TextMessageItem(true)
        msg.content = "测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息"
        list.add(msg)

        msg = TextMessageItem(false)
        msg.content = "测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息"
        list.add(msg)

        msg = TextMessageItem(false)
        msg.content = "测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息"
        list.add(msg)

        msg = TextMessageItem(false)
        msg.content = "测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息"
        list.add(msg)

        msg = TextMessageItem(false)
        msg.content = "测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息"
        list.add(msg)

        msg = TextMessageItem(false)
        msg.content = "测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息"
        list.add(msg)

        msg = TextMessageItem(false)
        msg.content = "测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息测试文本消息"
        list.add(msg)


        return list
    }
}